package com.purchase.order.service.Impl;

import com.purchase.common.utils.SecurityUtils;
import com.purchase.common.utils.uuid.IdUtils;
import com.purchase.order.domain.MaterialType;
import com.purchase.order.mapper.MaterialMapper;
import com.purchase.order.service.IMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaterialServiceImpl implements IMaterialService {

    @Autowired
    MaterialMapper materialMapper;

    //添加物资大类
    @Override
    public void addMaterial(String name) {
        if(materialMapper.selectCount(name) !=0){
            throw new SecurityException("该物资已存在");
        }
        if (materialMapper.insertMaterialType(String.valueOf(IdUtils.randomSnowflake()),name) != 1){
            throw new SecurityException("新增失败");
        }
    }

    //修改物资信息
    @Override
    public void putMaterial(String id,String name) {
        MaterialType materialType = new MaterialType();
        materialType.setMaterialTypeId(id);
        materialType.setMaterialTypeName(name);
        if(materialMapper.updateMaterial(materialType) !=1){
            throw new SecurityException(materialType.getMaterialTypeName()+"物资修改失败");
        }
    }

    //删除物资
    @Override
    public void deleteMaterial(String id) {
        if(materialMapper.deleteMaterial(id) !=1){
            throw new SecurityException("删除失败");
        }
    }

    //获取物资列表
    @Override
    public List<MaterialType> getMaterial(String name) {
        if (name != null ){
            name = name.trim();
            if ( "undefined".equals(name)){
                name = null;
            }
        }
        return materialMapper.getMaterial(name);
    }

    @Override
    public List<MaterialType> getAll() {
        return materialMapper.getAlllist();
    }
}
